package net.zjitc.dao.Impl;

import org.apache.commons.dbutils.QueryRunner;
import utils.dbutils.C3P0utils;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * Created by Administrator on 2017/12/13.
 */
public class TransactionTest {
    public static void main(String[] args) {
        Connection conn=null;
        QueryRunner runner=new QueryRunner();
        try {
            conn= C3P0utils.getConnection();
            conn.setAutoCommit(false);
            String sql1="update account set money =money-100 where name='a'";
            String sql2="update account set money =money+100 where name='b'";
            runner.update(conn,sql1);
            int count=runner.update(conn,sql2);
            if(count==0)
            {
                conn.rollback();
            }
            conn.commit();
            conn.close();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
    }
}
